package cn.maxwell.order.controller;

import cn.maxwell.basic.util.JsonResult;
import cn.maxwell.basic.util.LoginContext;
import cn.maxwell.basic.util.PageList;
import cn.maxwell.order.domain.OrderPetAcquisition;
import cn.maxwell.order.dto.OrderDto;
import cn.maxwell.order.query.OrderPetAcquisitionQuery;
import cn.maxwell.order.service.IOrderPetAcquisitionServer;
import cn.maxwell.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderPetAcquisition")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class OrderPetAcquisitionController {

    @Autowired
    private IOrderPetAcquisitionServer orderPetAcquisitionServer;

    /**
     * 查询所有
     * @return
     */
    @GetMapping //get请求
    @ApiOperation(value = "查询所有" )
    public List<OrderPetAcquisition> loadAll(){
        return orderPetAcquisitionServer.loadAll();
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}") //get请求，携带id参数
    @ApiOperation(value = "通过ID查询" )
    //swagger注解：paramType="path"：解析path路径携带的参数；dataType="Long"：指定参数类型
    @ApiImplicitParam(name="id",value="编号",required = true,paramType="path",dataType="Long")
    public OrderPetAcquisition loadById(@PathVariable("id") Long id){//@PathVariable：获取path路径携带的参数
        return orderPetAcquisitionServer.loadById(id);
    }

    /**
     * 添加或修改
     * @RequestBody：从请求体中拿到数据
     * @param orderPetAcquisition
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "添加或修改操作" )
    public JsonResult save(@RequestBody OrderPetAcquisition orderPetAcquisition){//@RequestBody:在请求体中获取参数
        try {
            if(orderPetAcquisition.getId()==null){
                orderPetAcquisitionServer.add(orderPetAcquisition);
            }else{
                orderPetAcquisitionServer.update(orderPetAcquisition);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public JsonResult patchDelete(@RequestBody List<Long> ids){ //List<Long>:要删除的id的集合
        try {
            orderPetAcquisitionServer.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 根据登录人employee查看收购订单，平台管理员查看所有收购订单，店铺管理员查看自己店铺收购订单，店员查看自己订单
     * 接口：分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {
        return orderPetAcquisitionServer.queryPage(query,request);
    }

    /**
     * 确认订单
     * @param orderDto
     * @return
     */
    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody OrderDto orderDto){
        try {
            orderPetAcquisitionServer.confirm(orderDto);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("确认订单失败");
        }
    }

    /**
     *  确认订单
     * @return
     */
    @ApiOperation(value = "取消订单接口" )
    @DeleteMapping("/{id}")
    public JsonResult cancel(@PathVariable("id") Long id){
        try {
            // 哈哈哈
            orderPetAcquisitionServer.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单取消失败！");
        }
    }
}
